package com.yinhe.module.cjl;

import com.yinhe.common.annotation.Log;
import com.yinhe.common.core.controller.BaseController;
import com.yinhe.common.core.domain.AjaxResult;
import com.yinhe.common.core.domain.entity.SysUser;
import com.yinhe.common.core.page.TableDataInfo;
import com.yinhe.common.enums.BusinessType;
import com.yinhe.common.exception.ServiceException;
import com.yinhe.common.utils.poi.ExcelUtil;
import com.yinhe.system.domain.YhContract;
import com.yinhe.system.domain.YhContractSecond;
import com.yinhe.system.service.ISysUserService;
import com.yinhe.system.service.IYhContractSecondService;
import com.yinhe.system.service.IYhContractService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 合同Controller
 * 
 * @author yinhe
 * @date 2022-09-26
 */
@RestController
@RequestMapping("/module/cjl/second")
public class CjlContractSecondController extends BaseController
{
    @Autowired
    private IYhContractSecondService yhContractSecondService;
    @Autowired
    private ISysUserService userService;
    @Autowired
    private IYhContractService yhContractService;

    /**
     * 查询合同列表
     */
    @PreAuthorize("@ss.hasPermi('module:cjl:second:list')")
    @GetMapping("/list")
    public TableDataInfo list(YhContractSecond yhContractSecond)
    {
        startPage();
        List<YhContractSecond> list = yhContractSecondService.selectYhContractSecondList(yhContractSecond);
        return getDataTable(list);
    }

    /**
     * 导出合同列表
     */
    @PreAuthorize("@ss.hasPermi('module:cjl:second:export')")
    @Log(title = "合同", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, YhContractSecond yhContractSecond)
    {
        List<YhContractSecond> list = yhContractSecondService.selectYhContractSecondList(yhContractSecond);
        ExcelUtil<YhContractSecond> util = new ExcelUtil<YhContractSecond>(YhContractSecond.class);
        util.exportExcel(response, list, "合同台账数据");
    }

    /**
     * 获取合同详细信息
     */
    @PreAuthorize("@ss.hasPermi('module:cjl:second:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(yhContractSecondService.selectYhContractSecondById(id));
    }


    /**
     * 新增合同
     */
    @PreAuthorize("@ss.hasPermi('module:cjl:second:add')")
    @Log(title = "合同", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody YhContractSecond yhContractSecond, YhContract yhContract)
    {
        SysUser sysUser=userService.selectUserByNickName(yhContractSecond.getPersonLiable());
        //String deptId = Long.toString(sysUser.getDeptId());//转为字符串类型
        if(sysUser==null){
            throw new ServiceException("请确认主管人员是否填写正确！");
        }else {
            String deptId = Long.toString(sysUser.getDeptId());//转为字符串类型
            yhContractSecond.setDeptId(deptId);
            yhContract.setContractAssociationStatus("2");//已关联
            Long ID=Long.parseLong(yhContractSecond.getTaskId());
            yhContract.setId(ID);
            yhContractService.updateYhContract(yhContract);
        }
        //yhContractSecond.setDeptId(deptId);
        return toAjax(yhContractSecondService.insertYhContractSecond(yhContractSecond));
    }

    /**
     * 修改合同
     */
    @PreAuthorize("@ss.hasPermi('module:cjl:second:edit')")
    @Log(title = "合同", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody YhContractSecond yhContractSecond)
    {
        return toAjax(yhContractSecondService.updateYhContractSecond(yhContractSecond));
    }

    /**
     * 删除合同
     */
    @PreAuthorize("@ss.hasPermi('module:cjl:second:remove')")
    @Log(title = "合同", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(yhContractSecondService.deleteYhContractSecondByIds(ids));
    }
}
